import axios, { AxiosResponse, AxiosRequestConfig } from "axios";
axios.defaults.withCredentials = true;
const service = axios.create({
  baseURL: "http://localhost:8083",
  timeout: 50000,
});

service.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    const token = localStorage.getItem("token");
    if (token && config.headers) {
      config.headers["token"] = token;
    }
    return config;
  },
  (error: any) => {
    Promise.reject(error);
  }
);

service.interceptors.response.use(
  async (response: AxiosResponse) => {
    // do something
    if (response.status === 200) {
      return response.data;
    } else {
      Promise.reject();
    }
  },
  (error: any) => {
    // do something
    return Promise.reject(error);
  }
);

export default service;
